#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
__title__ = ''
__author__ = 'HaiFeng'
__mtime__ = '2016/8/16'
"""
import talib
from py_at.Enums import *
from py_at.Data import Data
from py_at.Bar import Bar
from py_at.strategy import Strategy

import time

class MyLong(Strategy):
    def __init__(self,cfg):
        super().__init__(cfg)
        # 初始化默认参数
        if cfg=="":
            self.Params['MA1'] = 200
            self.Params['MA2'] = 200
            self.Params['Lots'] = 1
            self.Params['Slippage'] = 0
        self.p_ma1 = self.Params['MA1']
        self.p_ma2 = self.Params['MA2']
        self.p_lots = self.Params['Lots']
        self.Slippage = self.Params['Slippage']
        self.timeD=''

    def OnBarUpdate(self, data=Data, bar=Bar):
        # print('时间{0}收盘价格{1}总k线数据'.format(self.D, self.C, len(self.C)))
        # if len(self.C) == 1:       # 第一根bar
        #     self.UpdateParams()

        if len(self.C) < self.p_ma2:     # 如果bar数目小于 p._ma2参数  退出
            return

        ma1 = talib.MA(self.H, self.p_ma1)
        ma2 = talib.MA(self.L, self.p_ma2)
        if(self.timeD!=self.D[-1]):
            # self.writeStrategyLog("时间{0}开{1} 高{2} 低{3} 收{4}合约{5}".format(self.D[-2],self.O[-2],self.H[-2],self.L[-2],self.C[-2],self.Instrument))
            # self.writeStrategyLog("开%s 高%s 低%s 收%s 时间%s bar 合约%s" % (self.Bars[-2].O, self.Bars[-2].H, self.Bars[-2].L, self.Bars[-2].C, self.D[-2],self.Instrument))
            # self.writeStrategyLog("ID%s上根时间|%s时间ma均线%s ma2均线%s 合约%s" % (self.ID,self.D[-2], ma1[-2], ma2[-2],self.Instrument))
            #self.writeStrategyLog("ID%s当前多仓%s当前空仓%s" % (self.ID,self.PositionLong, self.PositionShort))

            self.timeD = self.D[-1]
            # highest = self.H[-self.p_ma1*-1:-1].max()
            # lowest=self.L[-self.p_ma1*-1:-1].min()
            self.IndexDict['Hma'] = ma1.tolist()
            self.IndexDict['Lma'] = ma2.tolist()
        if self.PositionLong == 0:    # 多仓等于0  上传
            if self.C[-2]>ma1[-2] and self.C[-3]>ma1[-3] and self.C[-4]>ma1[-4] and self.C[-5]>ma1[-5] :
                #self.writeStrategyLog("已经上传 %s"  %self.timeD);
                if self.PositionShort > 0:     # 如果有空仓
                    #self.writeStrategyLog("当前仓位为-1所以先平掉 买平");
                    self.BuyToCover(self.O[-1]+self.Slippage*self.PriceTick, self.p_lots, '买平')  # 平空 当前k收盘
                self.Buy(self.O[-1]+self.Slippage*self.PriceTick, self.p_lots, '买开')     # 买多
                #self.writeStrategyLog("{0}开盘价格{1}开多 加上滑点为{2}".format(self.D[-1],self.O[-1],self.O[-1]+self.Slippage*self.PriceTick))
        elif self.PositionShort == 0:
            if self.C[-2]<ma2[-2] and self.C[-3]<ma2[-3] and self.C[-4]<ma2[-4] and self.C[-5]<ma2[-5]  :
                #self.writeStrategyLog("已经下传 %s" %self.timeD);
                if self.PositionLong > 0:
                    #self.writeStrategyLog("当前仓位为1所以先平掉 卖平");
                    self.Sell(self.O[-1]-self.Slippage*self.PriceTick, self.p_lots, '卖平')
                self.SellShort(self.O[-1]-self.Slippage*self.PriceTick, self.p_lots, '卖开')
                #self.writeStrategyLog("{0}开盘价格{1}开空 加上滑点为{2}".format(self.D[-1], self.O[-1],self.O[-1]-self.Slippage*self.PriceTick))

